package com.smilive.start.netty.chat.peotocol;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: netty
 * @Date: 2022-01-10 14:39
 * @Author: smilive
 * @Description: 空闲检测: 添加IdleStateHandler对空闲时间进行检测，通过构造函数可以传入三个参数
 * readerIdleTimeSeconds 读空闲经过的秒数
 * writerIdleTimeSeconds 写空闲经过的秒数
 * allIdleTimeSeconds 读和写空闲经过的秒数
 */
@Slf4j
public class ServerChannelDuplexHandler extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        // 获得事件
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            // 断开连接
            ctx.channel().close();
        }
    }
}
